package main

import (
	"fmt"
	"strings"
)

/*
管道：数据流式处理，每个阶段专注一个任务
*/
func main() {
	// 数据源
	input := make(chan string)
	go func() {
		defer close(input)
		words := []string{"hello", "world", "golang", "channel", "pipeline"}
		for _, word := range words {
			input <- word
		}
	}()

	// 第一阶段：转换为大写
	upperStage := func(in <-chan string) <-chan string {
		out := make(chan string)
		go func() {
			defer close(out)
			for word := range in {
				out <- strings.ToUpper(word)
			}
		}()
		return out
	}

	// 第二阶段：添加前缀
	prefixStage := func(in <-chan string) <-chan string {
		out := make(chan string)
		go func() {
			defer close(out)
			for word := range in {
				out <- "PREFIX_" + word
			}
		}()
		return out
	}

	// 第三阶段：添加后缀
	suffixStage := func(in <-chan string) <-chan string {
		out := make(chan string)
		go func() {
			defer close(out)
			for word := range in {
				out <- word + "_SUFFIX"
			}
		}()
		return out
	}

	// 构建管道
	stage1 := upperStage(input)
	stage2 := prefixStage(stage1)
	stage3 := suffixStage(stage2)

	// 收集结果
	fmt.Println("=== 管道处理结果 ===")
	for result := range stage3 {
		fmt.Println(result)
	}
}
